﻿using Basic.FlowApplication.Config;
using Basic.FlowApplication.Interface;
using Basic.FlowApplication.Model;
using Basic.FlowApplication.Model.AuditLog;
using Basic.FlowExtendService;
using Basic.FlowRemoteModel;
using Basic.HrRemoteModel;
using Basic.HrRemoteModel.Msg.Model;
using LinqKit;

namespace Basic.FlowApplication.Extend
{
    internal class AuditNoticeMsgService : IAuditNoticeMsgService
    {
        private static readonly FlowAuditItemStatus[] _NoNoticeStatus = new FlowAuditItemStatus[]
        {
            FlowAuditItemStatus.审核进行中,
            FlowAuditItemStatus.已作废,
            FlowAuditItemStatus.待开始
        };
        private readonly IMsgService _Msg;
        private readonly IFlowAppService _App;
        private readonly IEmpService _Emp;
        public AuditNoticeMsgService ( IMsgService msg, IFlowAppService app, IEmpService emp )
        {
            this._Emp = emp;
            this._App = app;
            this._Msg = msg;
        }
        private readonly List<MsgAddItem> _Items = new List<MsgAddItem>();
        private AuditLog _Log;
        private AfootAuditLogItem _Item;
        private FlowNode _Node;
        private string _AppName;
        private string _EmpName;
        private void _NoticeInitiator ()
        {
            string title = string.Format("你的【{0}】审批状态已变更!", this._Log.DocTitle);
            this._Items.Add(new MsgAddItem
            {
                EmpId = this._Item.EmpId,
                FormEmpId = this._Log.CreateBy,
                LinkPk = this._Log.BeginItemId,
                MsgTitle = title,
                Priority = HrMsgPriority.普通,
                Attrs = new Dictionary<string, object>
                {
                    {"NodeName", this._Node.NodeName },
                    {"AuditType", (int)this._Node.AuditType },
                    {"AppName", this._AppName},
                    {"Status", (int)this._Item.Status }
                 }
            });
        }
        private void _NoticeNodeEmp ()
        {
            string title = string.Format("{0}发起的【{1}】审批状态已变更!", this._EmpName, this._Log.DocTitle);
            this._Log.Items.ForEach(c =>
            {
                if ( _NoNoticeStatus.Contains(c.Status) == false )
                {
                    this._Items.Add(new MsgAddItem
                    {
                        EmpId = this._Item.EmpId,
                        FormEmpId = c.EmpId,
                        LinkPk = c.Id,
                        MsgTitle = title,
                        Priority = HrMsgPriority.普通,
                        Attrs = new Dictionary<string, object>
                        {
                            {"NodeName", this._Node.NodeName },
                            {"AuditType", (int)this._Node.AuditType },
                            {"AppName", this._AppName},
                            {"Status", (int)this._Item.Status }
                         }
                    });
                }
            });
        }

        private void _NoticeBeginAudit ()
        {
            string title = string.Format("您有新的工作需要审批：【{0}】，发起人: {1}", this._Log.DocTitle, this._EmpName);
            this._Log.Items.ForEach(c =>
            {
                if ( c.Status == FlowAuditItemStatus.审核进行中 )
                {
                    this._Items.Add(new MsgAddItem
                    {
                        EmpId = this._Item.EmpId,
                        FormEmpId = c.EmpId,
                        LinkPk = c.Id,
                        MsgTitle = title,
                        Priority = HrMsgPriority.普通,
                        Attrs = new Dictionary<string, object>
                        {
                            {"NodeName", this._Node.NodeName },
                            {"AuditType", (int)this._Node.AuditType },
                            {"AppName", this._AppName},
                            {"Status", (int)this._Item.Status }
                         }
                    });
                }
            });
        }
        public void HandlerMsg ( AuditLog log, AfootAuditLogItem item, AuditFlowBody flow )
        {
            this._Item = item;
            this._Log = log;
            this._AppName = this._App.GetAppName(log.AppId);
            this._Node = flow.Nodes[item.NodeId];
            this._EmpName = this._Emp.GetName(this._Log.CreateBy);
            if ( this._Node.NoticeRange != FlowMsgNoticeRange.不通知 && _NoNoticeStatus.Contains(item.Status) == false )
            {
                if ( ( FlowMsgNoticeRange.发起人 & this._Node.NoticeRange ) == FlowMsgNoticeRange.发起人 )
                {
                    this._NoticeInitiator();
                }
                if ( ( FlowMsgNoticeRange.经办人 & this._Node.NoticeRange ) == FlowMsgNoticeRange.经办人 )
                {
                    this._NoticeNodeEmp();
                }
            }
            this._NoticeBeginAudit();
            if ( this._Items.Count > 0 )
            {
                MsgAdd add = new MsgAdd
                {
                    MsgType = HrMsgType.普通信息,
                    LinkType = MsgConstDic.MsgLinkType,
                    AppId = log.AppId,
                    MsgClassify = MsgConstDic.NoticeMsgClassify,
                    Items = this._Items.ToArray()
                };
                this._Msg.SendMsg(add);
            }
        }
    }
}
